﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace Tencent.OA.App
{
    public class TrademarkEntity
    {
        public string FileName { get; set; }
        public string Title { get; set; }
    }

    public class IcoMapping
    {
        //private static string _path = HttpContext.Current.Server.MapPath("~/Content/" + TrademarkName);
        //private static string _configPath = System.IO.Path.Combine(_path, "IcoConfig.xml");
        public static IList<TrademarkEntity> GetAllTrademark(string trademarkName)
        {
            List<TrademarkEntity> result = new List<TrademarkEntity>();
            //string path = HttpContext.Current.Server.MapPath("~/Content/icoTrademarkhelp");
            string path = HttpContext.Current.Server.MapPath("~/Content/Resource/" + trademarkName);
            string configPath = System.IO.Path.Combine(path, "IcoConfig.xml");
            if (!System.IO.File.Exists(configPath))
                return result;

            XElement doc = XElement.Load(configPath);
            var query = from x in doc.Descendants("item")
                        select new TrademarkEntity
                        {
                            FileName = x.Attribute("fileName").Value,
                            Title = x.Attribute("title").Value
                        };
            return query.ToList<TrademarkEntity>();
        }
        /// <summary>
        /// 更新商标XML文件
        /// </summary>
        /// <param name="te"></param>
        /// <param name="trademarkName">商标类型名</param>
        /// <returns></returns>
        public static bool UpdateTrademark(List<TrademarkEntity> te, string trademarkName)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("~/Content/" + trademarkName);
                string configPath = System.IO.Path.Combine(path, "IcoConfig.xml");

                if (!System.IO.File.Exists(configPath))
                    return false;

                XElement doc = XElement.Load(configPath);
                //删除
                var query = from o in doc.Descendants("item")
                            select o;
                query.Remove();

                foreach (var o in te)
                {
                    //新增
                    XElement newItem = new XElement("item",
                                          new XAttribute("fileName", o.FileName),
                                          new XAttribute("title", o.Title)
                                      );

                    doc.Element("mappingSection").Add(newItem);
                }
                //保存xml
                doc.Save(configPath);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 得到商标涉及国家名称
        /// </summary>
        /// <returns></returns>
        public static string GetNations()
        {
            string configPath = HttpContext.Current.Server.MapPath("~/Content/Resource/Config.xml");

            if (!System.IO.File.Exists(configPath))
                return string.Empty;

            XElement doc = XElement.Load(configPath);
            var query = from o in doc.Descendants("trademarkNation")
                        select new
                        {
                            NationName = o.Attribute("NationName").Value
                        };

            if (query != null && query.Count() > 0)
            {
                return query.FirstOrDefault().NationName;
            }
            return string.Empty;
        }

        /// <summary>
        /// 更新商标涉及国家名称
        /// </summary>
        /// <param name="te"></param>
        /// <param name="trademarkName">商标类型名</param>
        /// <returns></returns>
        public static bool UpdateNations(string nationName)
        {
            try
            {
                string configPath = HttpContext.Current.Server.MapPath("~/Content/Resource/Config.xml");
                if (!System.IO.File.Exists(configPath))
                    return false;

                XElement doc = XElement.Load(configPath);
                //删除
                var query = from o in doc.Descendants("trademarkNation")
                            select o;
                query.Remove();

                //新增
                XElement newItem = new XElement("trademarkNation",
                                      new XAttribute("NationName", nationName)
                                  );

                doc.Element("mappingSection").Add(newItem);

                //保存xml
                doc.Save(configPath);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}